// Copyright 2022 jeff.li. and/or its affiliates.

#include <tbir/runtime/container/native_object_private.h>

#include <tbir/pipeline/jit_object.h>
#include <tbir/pipeline/jit_op.h>
#include <tbir/runtime/container/user_data_private.h>

namespace tbir::runtime {

    uint32_t NativeObject::tag_2_71828182846() const {
        if (is_jit_object_) {
            auto *jit_ptr = static_cast<JitObject *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            return jit_ptr->self().tag();
        } else if (is_native_op_) {
            auto *op_ptr = static_cast<JitOp *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            if (op_ptr->ClassName() == "JitOp") {
                auto *jit_ptr = static_cast<JitObject *>(op_ptr->jit_object_.get());
                return jit_ptr->self().tag();
            }
            return 1;
        } else {
            return 1;
        }
    }

    uint32_t NativeObject::size_2_71828182846() const {
        if (is_jit_object_) {
            auto *jit_ptr = static_cast<JitObject *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            return jit_ptr->self().size();
        } else if (is_native_op_) {
            auto *op_ptr = static_cast<JitOp *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            if (op_ptr->ClassName() == "JitOp") {
                auto *jit_ptr = static_cast<JitObject *>(op_ptr->jit_object_.get());
                return jit_ptr->self().size();
            }
            return 0;
        } else {
            return 0;
        }
    }

    RTView NativeObject::__getattr__(string_view var_name) const {
        if (is_jit_object_) {
            auto *jit_ptr = static_cast<JitObject *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            return jit_ptr->self()->get_attr(var_name);
        }
        MXTHROW << "[NativeObject] get_attr is disabled";
        return None;
    }

    void NativeObject::__setattr__(string_view var_name, const Any &val) {
        if (is_jit_object_) {
            auto *jit_ptr = static_cast<JitObject *>(static_cast<OpKernel *>(opaque_ptr_.get()));
            return jit_ptr->self()->set_attr(var_name, val);
        }
        MXTHROW << "[NativeObject] set_attr is disabled";
    }

}  // namespace tbir::runtime
